import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path*',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
]

export const asyncRoutes = [
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '讲师首页', icon: 'dashboard', affix: true }
    }]
  },

  // 课程管理
  {
    path: '/manage/course',
    component: Layout,
    redirect: '/manage/course/list',
    name: 'course',
    meta: { title: '课程管理', icon: 'form' },
    children: [
      {
        path: 'list',
        name: 'courseList',
        component: () => import('@/views/course/list'),
        meta: { title: '课程列表' }
      },
      {
        path: 'info',
        name: 'courseInfo',
        component: () => import('@/views/course/info'),
        meta: { title: '发布课程' }
      },
      {
        path: 'info/:id',
        name: 'courseInfoEdit',
        component: () => import('@/views/course/info'),
        meta: { title: '编辑课程基本信息', noCache: true },
        hidden: true
      },
      {
        path: 'chapter/:id',
        name: 'courseChapterEdit',
        component: () => import('@/views/course/chapter'),
        meta: { title: '编辑课程大纲', noCache: true },
        hidden: true
      },
      {
        path: 'publish/:id',
        name: 'coursePublishEdit',
        component: () => import('@/views/course/publish'),
        meta: { title: '发布课程', noCache: true },
        hidden: true
      }
    ]
  },

  //评论管理
  {
    path: '/manage/consult',
    component: Layout,
    alwaysShow: true,
    redirect: '/manage/consult/list',
    name: 'consult',
    meta: { title: '课程咨询管理', icon: 'tree' },
    children: [
      {
        path: 'list',
        name: 'consultList',
        component: () => import('@/views/consult/list'),
        meta: { title: '咨询列表' }
      }
    ]
  },

  //数据报表管理
  {
    path: '/manage/daily',
    alwaysShow: true,
    component: Layout,
    redirect: '/manage/daily/list',
    name: 'daily',
    meta: { title: '数据报表', icon: 'chart' },
    children: [
      {
        path: 'list',
        name: 'dailyList',
        component: () => import('@/views/daily/list'),
        meta: { title: '数据报表' }
      },
    ]
  },

  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  mode: 'history',
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher
}

export default router
